import 'package:flutter/material.dart';
import 'package:talk/pages/add/add_qr.dart';
import 'package:talk/src/utils/theme_utils.dart';

import '../../../home/AppLocale.dart';
import '../../../res/colors.dart';
import '../../../routers/fluro_navigator.dart';
import '../../../tools/widgets/load_image.dart';
import '../../add/add.dart';
import '../../add/x_add_router.dart';
import '../../device/ScanPage.dart';


class GoodsAddMenu extends StatefulWidget {
  const GoodsAddMenu({super.key});

  @override
  _GoodsAddMenuState createState() => _GoodsAddMenuState();
}

class _GoodsAddMenuState extends State<GoodsAddMenu> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 200),
      vsync: this,
    );

    _scaleAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller);
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  // 处理扫码添加按钮点击事件
  Future<void> _handleScanButtonPressed() async {
    // 保存当前路由名称，用于返回时判断
    final currentRouteName = ModalRoute.of(context)?.settings.name ?? '';

    // 跳转到 ScanPage 并等待结果
    final scanResult = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => const ScanPage()),
    );

    if (scanResult != null && mounted) {
      // 关闭 GoodsAddMenu 所在的页面
      Navigator.pop(context);

      // 跳转到 AddPage 并传递扫描结果（使用普通 push 而不是 pushReplacement）
      Navigator.push(
        context,
        MaterialPageRoute(
          // builder: (context) => AddPage(scanResult: scanResult.toString()),
          builder: (context) => AddQrPage(scanResult: scanResult.toString()),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final Color backgroundColor = context.backgroundColor;
    final Color? iconColor = ThemeUtils.getIconColor(context);

    final Widget body = Column(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.only(right: 12.0),
          child: LoadAssetImage('images/goods/jt', width: 8.0, height: 4.0,
            color: ThemeUtils.getDarkColor(context, Colours.dark_bg_color),
          ),
        ),
        SizedBox(
          width: 120.0,
          height: 40.0,
          child: TextButton.icon(
            onPressed: _handleScanButtonPressed, // 修改为处理函数
            icon: LoadAssetImage('images/goods/goods_saoma', width: 25.0, height: 25.0, color: iconColor,),
            label: Text(AppLocale.text('d_saomatianjia')),
            style: TextButton.styleFrom(
              foregroundColor: Theme.of(context).textTheme.bodyMedium?.color,
              disabledForegroundColor: Theme.of(context).textTheme.bodyMedium?.color?.withOpacity(0.12),
              backgroundColor: Colors.white,
              shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.only(topLeft: Radius.circular(8.0), topRight: Radius.circular(8.0)),
              ),
            ),
          ),
        ),
        SizedBox(
          width: 120.0,
          height: 40.0,
          child: TextButton.icon(
            onPressed: () {
              NavigatorUtils.push(context, '${XAddRouter.addPage}', replace: true);
            },
            icon: LoadAssetImage('images/goods/goods_compoter', width: 25.0, height: 25.0, color: iconColor,),
            label: Text(AppLocale.text('d_tianjiashebei')),
            style: TextButton.styleFrom(
              foregroundColor: Theme.of(context).textTheme.bodyMedium?.color,
              disabledForegroundColor: Theme.of(context).textTheme.bodyMedium?.color?.withOpacity(0.12),
              backgroundColor: Colors.white,
              shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.only(bottomLeft: Radius.circular(8.0), bottomRight: Radius.circular(8.0)),
              ),
            ),
          ),
        ),
        Container(width: 120.0, height: 0.6, color: Colours.line),
      ],
    );

    return AnimatedBuilder(
      animation: _scaleAnimation,
      builder: (_, child) {
        return Transform.scale(
          scale: _scaleAnimation.value,
          alignment: Alignment.topRight,
          child: child,
        );
      },
      child: body,
    );
  }
}